<?php
/**
 * 定义editor.*控件
 * 
 * 目前支持的Javascript Editor有：
 * - <editor:fck/> - fckeditor 需要先从http://www.fckeditor.net安装
 * 
 * Fckeditor 支持的参数有，都不是必需的：
 * - name <string> 生成的表单元素名，默认为fckeditor
 * - base <string> 编辑器所在根目录，默认为/fckeditor/
 * - width <integer> 编辑器宽度
 * - height <integer> 编辑器高度
 * - toolbar <string> 工具栏样式
 * - value <scalar> 初始值
 * - config.* <array> 选项配置
 *
 * @package if
 * @subpackage module.ui
 * @since 1.1.0
 */
class UIEditor extends UIFormElement {
	function request($name, $group = false) {
		if (preg_match("/^(.+)\[(.*)\]/U", $name, $match)) {
			$param = $match[1];
			$value = xn($param);
			if ($group) {
				return $value;
			}
			if ($match[2] === " " || $match[2] === "") {
				return $value;
			}
			if (is_array($value) && array_key_exists($match[2], $value)) {
				return $value[$match[2]];
			}
			return null;
		}
		return xn($name);
	}
	
	function render() {
		list($prefix, $name) = explode(":", $this->name());
		$renderFunc = "_render" . $name;
		if (method_exists($this, $renderFunc)) {
			return $this->$renderFunc();
		}
	}
	
	private function _renderFck() {
		//name
		if (!$this->attr("name")) {
			$this->setAttr("name", "fckeditor");
		}
		
		if (!class_exists("FCKEditor", false)) {
			require dirname(__FILE__) . "/fck/FckEditor.php";
		}
		$editor = new FCKeditor($this->attr("name"));
		
		//base 
		$base = $this->attr("base");
		if (!$base) {
			$base = __BASE__ . "/fckeditor/";
		}
		$editor->BasePath = $base;
		
		if ($this->attr("width")) {
			$editor->Width = $this->attr("width");
		}
		if ($this->attr("height")) {
			$editor->Height = $this->attr("height");
		}
		if ($this->attr("toolbar")) {
			$editor->ToolbarSet = $this->attr("toolbar");
		}
		if ($this->attr("value")) {
			$editor->Value = $this->attr("value");
		}
		
		$config = $this->attr("config");
		if (is_array($config)) {
			$editor->Config = $config;
		}
		
		return $editor->CreateHtml() . $this->renderError();
	}
}

?>